package com.lxf.cn.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lxf.cn.config.BaseController;
import com.lxf.cn.redis.RedisUitls;
import com.lxf.cn.service.UserService;
import com.lxf.cn.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liaoxuefeng on 2018/11/2.
 */

//RestController 和 Controller 区别
//RestController 相当于  Controller+ResponseBody一起使用
//RestController 不返回jsp页面
@Controller
@RequestMapping("/rest")
public class RestfulController extends BaseController {




    @Autowired
    private UserService userService;

    @Autowired
    WebSocketServer webSocketServer ;

    @Autowired
    RedisUitls redisUitls ;


    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public  @ResponseBody  Object redis(){
        /**
         * 分布式锁基于redis setnx 实现
         */
        String index = redisUitls.lockWithTimeout("lockName" , 500 , 10000);
        System.out.printf("---->"+index);
        if(StringUtils.isEmpty(index)){
            throw new RuntimeException("请勿重复提交!");
        }else{
            //业务处理
            //释放分布式锁
           // redisUitls.releaseLock("lockName" ,index);
            return null;
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody
    Object saveUser(@RequestBody JSONObject params) {

        String userName = params.getString("userName");
        Assert.notNull(userName, "用户名称不能为空");

        String password = params.getString("password");
        Assert.notNull(password, "用户密码不能为空");

        String phone = params.getString("phone");
        Assert.notNull(phone, "用户手机号码不能为空");

        return userService.save(params) ;
    }


    @RequestMapping(value = "/user/search" , method = RequestMethod.POST)
    public @ResponseBody Object searchUser(@RequestBody JSONObject params){

       Integer pageNum =  params.getInteger("pageNum");
       Integer pageSize = params.getInteger("pageSize");
       PageInfo pageInfo =  userService.findAllUser( pageNum, pageSize, params);
       return pageInfo ;
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @RequestMapping(value = "/user/update" , method = RequestMethod.POST)
    public @ResponseBody Object updateUserByIds(@RequestBody JSONObject params){
        return userService.updateUserByIds(params) ;
    }




    @RequestMapping(value = "/get/search" , method = RequestMethod.POST)
    public @ResponseBody Object getUser(@RequestBody JSONObject params){

        return userService.getExcleDate();

    }



    @RequestMapping(value = "/index" , method = RequestMethod.GET)
    public String index(){
        return "websocket.html";
    }


    /**
     * web sockent
     * @param param
     * @return
     */
    @RequestMapping(value="/pushVideoListToWeb",method=RequestMethod.POST)
    public @ResponseBody void pushVideoListToWeb(@RequestBody JSONObject param) {
        try {
            webSocketServer.sendInfo("有新客户呼入,sltAccountId:");
        }catch (IOException e) {
            e.printStackTrace();
        }

    }



}
